package nl.hu.verbrandendephoenix.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.ServerService;
import nl.hu.verbrandendephoenix.shared.Auction;
import nl.hu.verbrandendephoenix.shared.Bid;
import nl.hu.verbrandendephoenix.shared.Conversation;
import nl.hu.verbrandendephoenix.shared.Message;
import nl.hu.verbrandendephoenix.shared.User;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ServerServiceImpl extends RemoteServiceServlet implements ServerService {

	private static final long serialVersionUID = 1L;

	public static Connection getConnection() {
		Connection con = null;
		String userid="THO5_2011_3A_TEAM2";
		String password = "THO5_2011_3A_TEAM2";
		String url = "jdbc:oracle:thin:@ondora01.hu.nl:8521:cursus01";
		try {
			Class.forName ("oracle.jdbc.driver.OracleDriver");
		} 
		catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(url, userid, password);

		} 
		catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}

		return con;
	}
	@Override
	public boolean validLogin(String loginKey) {
		return UserMethods.validLogin(loginKey, getConnection());
	}
	@Override
	public boolean createUser(String unm, String pwd, String eml) {
		return UserMethods.createUser(unm, pwd, eml, getConnection());
	}
	@Override
	public String loginUser(String unm, String pwd) {
		return UserMethods.loginUser(unm, pwd, getConnection());
	}
	@Override
	public User getUserData(String loginKey) {
		return UserMethods.getUserData(loginKey, getConnection());
	}
	@Override
	public User getUserData(String loginKey, String username) {
		return UserMethods.getUserData(loginKey, username, getConnection());
	}
	@Override
	public boolean setUserData(String loginKey, User u) {
		return UserMethods.setUserData(loginKey, u, getConnection());
	}
	@Override
	public ArrayList<String> searchUser(String strSearchInput) {
		return UserMethods.searchUser(strSearchInput, getConnection());
	}
	@Override
	public int getUserID(String loginKey){
		return UserMethods.getUserID(loginKey, getConnection());
	}
	@Override
	public boolean blockUser(String loginKey, String username) {
		return UserMethods.blockUser(loginKey, username, getConnection());
	}
	@Override
	public boolean isAdmin(String loginKey) {
		return UserMethods.isAdmin(loginKey, getConnection());
	}
	public String getUsername(String loginKey){
		return UserMethods.getUsername(loginKey, getConnection());
	}
	
	public User getUserFromID(int userID){
		return UserMethods.getUserFromID(userID, getConnection());
	}
	
	public User getUserData(int userID){
		return UserMethods.getUserData(userID, getConnection());
	}
	@Override
	public Auction getAuctionData(int auctionID) {
		return AuctionMethods.getAuctionData(auctionID, getConnection());
	}
	@Override
	public boolean createAuction(String name, String desc, int minBid, String userkey, String cat) {
		return AuctionMethods.createAuction(name, desc, minBid, userkey, cat, getConnection());
	}
	@Override
	public ArrayList<Auction> searchAuction(String prodN, String minPrice, String maxPrice, String cat) {
		return AuctionMethods.searchAuction(prodN, minPrice, maxPrice, cat, getConnection());
	}
	public ArrayList<Auction> getMyAuctions(String loginKey) {
		return AuctionMethods.getMyAuctions(loginKey, getConnection());
	}
	@Override
	public String[] getCategories() {
		Connection con = getConnection();
		String[] categoryArray = null;
		int categoryCounter = 0;
		String category = "";
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT COUNT(DISTINCT Category) FROM Auction");
			if(rs.next())
				categoryArray = new String[Integer.parseInt(rs.getString(1))];
			rs = stmt.executeQuery("SELECT DISTINCT Category from Auction");
			while(rs.next())
			{
				category = rs.getString("Category");
				categoryArray[categoryCounter] = category;
				categoryCounter++;
			}
			stmt.close();
			con.close();
		}
		catch(SQLException ex) {
			ex.printStackTrace();
		}
		return categoryArray;
	}
	@Override
	public boolean bidAuction(int aID, double bid, String key) {
		return BidMethods.bidAuction(aID, bid, key, getConnection());
	}
	@Override
	public Bid getLatestOffer(int aucID) {
		return BidMethods.getLatestOffer(aucID, getConnection());
	}
	@Override
	public ArrayList<Bid> getAllHighestBids() {
		return BidMethods.getAllHighestBids(getConnection());
	}
	@Override
	public int getAuctionOwnerID(int acID) {
		return AuctionMethods.getAuctionOwnerID(acID, getConnection());
	}
	@Override
	public boolean compareAuctionOwner(int acID, String LoginKey) {
		return AuctionMethods.compareAuctionOwner(acID, LoginKey, getConnection());
	}
	
	@Override
	public boolean editAuction(int acID, Auction a){
		return AuctionMethods.editAuction(acID, a, getConnection());
	}
	
	@Override
	public boolean deleteAuction(int acID){
		return AuctionMethods.deleteAuction(acID, getConnection());
	}
	
	@Override
	public boolean checkCredits(int acID, String LoginKey){
		return AuctionMethods.checkCredits(acID, LoginKey, getConnection());
	}
	
	@Override
	public boolean payCredits(int acID, String LoginKey){
		return AuctionMethods.payCredits(acID, LoginKey, getConnection());
	}
	
	@Override
	public boolean addCredits(int aantCreds, String LoginKey){
		return AuctionMethods.addCredits(LoginKey, aantCreds, getConnection());
	}
	
	@Override
	public boolean stopAuction(int acID){
		return AuctionMethods.stopAuction(acID, getConnection());
	}
	
	
	@Override
	public ArrayList<Bid> getAllBidsFromID(int acID){
		return StatMethods.getAllBidsFromID(acID, getConnection());
	}
	
	@Override
	public ArrayList<Auction> getTopAuctions(){
		return StatMethods.getTopAuctions(getConnection());
	}
	@Override
	public Bid getHighestBidDay(int day, int month, int year) {
		return StatMethods.getHighestBidDay(day, month, year, getConnection());
	}
	@Override
	public Bid getHighestBidMonth(int month, int year) {
		return StatMethods.getHighestBidMonth(month, year, getConnection());
	}
	@Override
	public Bid getHighestBidYear(int year) {
		return StatMethods.getHighestBidYear(year, getConnection());
	}
	
	@Override
	public boolean checkForBid(int aucID){
		return BidMethods.checkForBid(aucID, getConnection());
	}
	@Override
	public ArrayList<Conversation> getConversations(User u) {
		return ConversationMethods.getConversations(u, getConnection());
	}
	@Override
	public Conversation getConversationData(int index) {
		System.out.println("Server fetch");
		return ConversationMethods.getConversationData(index, getConnection());
	}
	@Override
	public Message storeMessage(int index, String text, User current_User, User conv_partner) {
		return ConversationMethods.storeMessage(index, text, current_User, conv_partner, getConnection());
	}
	
	@Override
	public boolean checkActive(int acID) {
		return AuctionMethods.checkActive(acID, getConnection());
	}
}